import platform
import os
import dearpygui.dearpygui as dpg
from roleMaker import RoleMaker

class Form:
    def __init__(self, width=370, height=400):
        self.width = width
        self.height = height
        self.maker = RoleMaker()
        self.attrTable = {}

        with dpg.font_registry():
            fontFile = 'msyhmono.ttf'
            fontSize = 16
            if platform.system() == 'Windows':
                fontFile = os.path.join('C:', 'Windows', 'Fonts', 'msyh.ttc')
                fontSize = 18
            with dpg.font(file=fontFile, size=fontSize, default_font=True):
                dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)

    def CB_reload(self):
        self.maker.reload()
        dpg.delete_item(self.winTable)
        self.createAttrTable()

    def CB_changeRoleType(self, sender, app_data, user_data):
        self.maker.reset(app_data)
        dpg.delete_item(self.winTable)
        self.createAttrTable()

    def CB_refresh(self):
        self.maker.refresh()
        for key, val in self.maker.table.items():
            label, value, isLock, attrType = val
            dpg.set_value(self.attrTable[key][1], value)

    def CB_save(self):
        self.maker.save()

    def CB_changeIsLock(self, sender, app_data, user_data):
        self.maker.table[user_data][2] = app_data, 

    def CB_changeValue(self, sender, app_data, user_data):
        self.maker.table[user_data][1] = app_data

    def createAttrTable(self):
        dpg.push_container_stack(self.mainWin)
        if True:
            # 显示
            self.winTable = dpg.add_table(header_row=False, borders_innerH=False, borders_outerH=False, borders_innerV=False, borders_outerV=False)
            dpg.push_container_stack(self.winTable)
            dpg.add_table_column()
            dpg.add_table_column()
            dpg.add_table_column()
            for key, val in self.maker.table.items():
                label, value, isLock, attrType = val
                self.attrTable[key] = []
                self.attrTable[key].append(dpg.add_text(label))
                dpg.add_table_next_column()

                if attrType == 'choice':
                    self.attrTable[key].append(dpg.add_combo(self.maker.cfg[self.maker.roleType][key]['value'], label='', width=-1, default_value=value, callback=self.CB_changeValue, user_data=key))
                elif attrType in ('str', 'range'):
                    self.attrTable[key].append(dpg.add_input_text(width=-1, label='', default_value=value, callback=self.CB_changeValue, user_data=key))
                dpg.add_table_next_column()

                self.attrTable[key].append(dpg.add_checkbox(label='锁定', callback=self.CB_changeIsLock, user_data=key, default_value=isLock))
                dpg.add_table_next_column()
            dpg.pop_container_stack()
        dpg.pop_container_stack()


    def main(self):
        lstRoleType = list(self.maker.cfg.keys())

        self.mainWin = dpg.add_window(label='RoleMaker', width=self.width, pos=(0, 0))
        dpg.push_container_stack(self.mainWin)
        # 功能栏
        with dpg.table(header_row=False, borders_innerH=False, borders_outerH=False, borders_innerV=False, borders_outerV=False):
            dpg.add_table_column()
            dpg.add_table_column()
            dpg.add_table_column()
            dpg.add_table_column()
            dpg.add_button(label="重载配置", width=-1, callback=self.CB_reload)
            dpg.add_table_next_column()
            dpg.add_combo(lstRoleType, label='', width=-1, default_value=lstRoleType[0],
                    callback=self.CB_changeRoleType)
            dpg.add_table_next_column()
            dpg.add_button(label="刷新", width=-1, callback=self.CB_refresh)
            dpg.add_table_next_column()
            dpg.add_button(label="保存", width=-1, callback=self.CB_save)

            self.createAttrTable()
        dpg.pop_container_stack()

        dpg.setup_viewport()
        dpg.set_viewport_width(self.width)
        dpg.set_viewport_height(self.height)
        dpg.set_viewport_min_width(self.width)
        dpg.set_viewport_min_height(self.height)
        dpg.set_primary_window(self.mainWin, True)
        dpg.start_dearpygui()

if __name__=='__main__':
    form = Form()
    form.main()
